<template>
	<view>
		<view class="navBar">
			<view class="left">在线商城</view>
			<view class="right" @click="toSearch">
				<input type="text" value="" placeholder="输入关键词" />
				<icon type="search" size="34rpx" color="#1296db"></icon>
			</view>
		</view>
		<swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,index) in banner" :key="index">
				<image :src="item.imgurl" mode="widthFix"></image>
			</swiper-item>
		</swiper>
		<view class="goodsList">
			<view class="item" v-for="(item,index) in goods" :key="index" @click="toDetail(item.goodsId)">
				<image :src="item.image" mode="widthFix"></image>
				<view class="goodsName-price">
					<view class="goodsName">{{item.goodsName}}</view>
					<view class="price">
						<text class="yuan">￥</text>
						<text>{{item.price.toFixed(2).split('.')[0]}}.</text>
						<text class="decimal">{{item.price.toFixed(2).split('.')[1]}}</text>
						<text class="sales">已售{{item.sales}}件</text>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" :icon-size="16" :content-text="contentText"></uni-load-more>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goods: [],
				banner: [],
				pageNum: 1,
				pageSize: 6,
				totalPage: 0,
				status: 'more',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
			}
		},
		onLoad() {
			this.getGoods();
			this.getBanner();
			this.getTotalPage();
			uni.removeStorageSync("Form");
		},
		onReachBottom() {
			if (this.pageNum >= this.totalPage) {
				uni.showToast({
					icon:"none",
					title: '没有下一页数据了'
				});
				this.status='noMore'
			}
			else{
				this.pageNum++;
				this.getGoods();
			}
			
		},
		onPullDownRefresh() {
			this.goods=[];
			this.pageNum = 1;
			this.getGoods();
		},
		methods: {
			getGoods() {
				this.status='loading';
				uni.request({
					url: 'http://localhost:8080/goods/findAllGoods',
					method: 'GET',
					data: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
					},
					success: res => {
						for (let index in res.data.records) {
							this.goods.push(res.data.records[index]);
						}
						uni.stopPullDownRefresh();
					},
					fail: () => {},
					complete: () => {}
				});
			},
			getBanner() {
				uni.request({
					url: 'http://localhost:8080/banner/findBanner',
					method: 'GET',
					data: {},
					success: res => {
						this.banner = res.data;
					},
					fail: () => {},
					complete: () => {}
				});
			},
			getTotalPage() {
				uni.request({
					url: 'http://localhost:8080/goods/findTotalPage',
					method: 'GET',
					data: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
					},
					success: res => {
						this.totalPage = res.data;
					},
					fail: () => {},
					complete: () => {}
				});
			},
			toDetail(id) {
				uni.navigateTo({
					url: '/pages/detail/detail?id=' + id,
				});
			},
			toSearch() {
				uni.navigateTo({
					url: '/pages/search/search',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}

		}
	}
</script>

<style lang="scss">
	.navBar {
		height: 500rpx;
		background-color: white;
		padding: 0 30rpx;
		padding-top: 20rpx;
		/* #ifdef MP-WEIXIN */
		padding-top: 100rpx;
		/* #endif */
		overflow: hidden;
		background: linear-gradient(to bottom, #1296db, #f7f7f7);

		.left {
			flex: 1;
			color: white;
			font-size: 40rpx;
			font-weight: 700;
		}

		.right {
			position: relative;
			margin-top: 20rpx;
			flex: 2;
		}

		input {
			padding: 10rpx 0rpx;
			padding-left: 60rpx;
			border: 1rpx solid #ccc;
			border-radius: 50rpx;
			font-size: 30rpx;
			background-color: white;
		}

		icon {
			position: absolute;
			left: 20rpx;
			top: 16rpx;
		}
	}

	.swiper {
		height: 550rpx;
		margin: 0 20rpx;
		margin-top: -320rpx;
		border-radius: 50rpx;
		overflow: hidden;

		image {
			width: 100%;
		}
	}

	.goodsList {
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex-wrap: wrap;
		margin: 0 20rpx;
		margin-bottom: 20rpx;

		.item {
			box-sizing: border-box;
			margin-top: 25rpx;
			width: 345rpx;
			height: 500rpx;
			border-radius: 20rpx;
			background-color: white;
			overflow: hidden;

		}

		image {
			width: 100%;
			border-radius: 20rpx 20rpx 0 0;
		}

		.goodsName-price {
			padding: 15rpx 20rpx;

			.goodsName {
				white-space: nowrap;
				text-overflow: ellipsis;
				overflow: hidden;
			}

			.price {
				display: flex;
				align-items: baseline;
				padding: 20rpx 0;
				font-size: 38rpx;
				font-weight: 700;
				color: red;
			}

			.yuan,
			.decimal {
				font-size: 32rpx;
			}

			.sales {
				color: #646566;
				margin-left: 10rpx;
				font-weight: normal;
				font-size: 28rpx;
				white-space: nowrap;
				text-overflow: ellipsis;
				overflow: hidden;
			}
		}
	}
</style>